/*
 * 上海远境文化传媒有限公司 版权所有
 */
package facade.base;

import facade.AppContext;
import helper.BeanHelper;
import models.base.Corp;
import models.base.EntityLog;
import models.base.AdminUser;
import models.enums.ClientType;
import org.apache.log4j.MDC;
import play.Logger;
import play.jobs.Job;
import play.mvc.Http;

/**
 * 实体日志Facade.
 */
public class EntityLogFacade {

    /**
     * 记录实体日志。
     */
    public static void log(String entityId, AdminUser user, ClientType clientType, String logMessage) {
        Logger.info("LOG06993: User(id:%s, clientType:%s)操作entity(id:%s): %s",
                BeanHelper.getModelId(user), clientType, entityId, logMessage);

        // 日志点.
        Object oTtid = MDC.get("ttid");
        String ttid = oTtid != null ? String.valueOf(oTtid) : null;
        // 请求地址.
        String requestHost = Http.Request.current() != null ? Http.Request.current().host : null;

        Corp corp = AppContext.currentCorp();
        new Job() {
            @Override
            public void doJob() {
                EntityLog.createLog(corp, entityId, user, clientType, logMessage, ttid, requestHost);
            }
        }.afterRequest();
    }
}
